using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RiceShop.Services;
using RiceShop.Extensions;

namespace RiceShop.Controllers
{
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class CouponsController : ControllerBase
    {
        private readonly ICouponService _couponService;

        public CouponsController(ICouponService couponService)
        {
            _couponService = couponService;
        }

        [HttpGet]
        public async Task<IActionResult> GetCoupons([FromQuery] byte? status)
        {
            var coupons = await _couponService.GetCouponsAsync(status);
            return Ok(coupons);
        }

        [HttpPost("{id}/receive")]
        public async Task<IActionResult> ReceiveCoupon(long id)
        {
            var userId = User.GetUserId();
            await _couponService.ReceiveCouponAsync(userId, id);
            return Ok();
        }

        [HttpPost("{id}/use")]
        public async Task<IActionResult> UseCoupon(long id)
        {
            var userId = User.GetUserId();
            await _couponService.UseCouponAsync(userId, id);
            return Ok();
        }
    }
}